define(["Zepto", 
        "underscore", 
        "Backbone", 
        "Handlebars",  
        "text!templates/corsolaurea/corsolaurea.html",
        "collections/corsocollection",
        "collections/utentecollection",
        "views/corsolaurea/coursesview",
        "views/corsolaurea/usersview",
        "models/corsolaurea",
        "views/common/menuview"
], function($, _, Backbone,  Handlebars, template, CorsoCollection, UtenteCollection, CoursesView, UsersView, Corsolaurea, MenuView) {
	
   var CorsolaureaPage = Backbone.View.extend({
		  
	        template: Handlebars.compile(template),
	        
	        initialize: function() {
	        	
	        },

	        render: function(eventName) {
	        	
	        	this.$el.html(this.template(this.model.toJSON()));
				
				// menu top
				this.menutop = new MenuView({
					el : $('#menu', this.el),
					model : this.model
				});
				this.menutop.render();

				// Corsi tenuti nel corso di laurea
				this.corsoCollection = new CorsoCollection();
				this.corsoCollection.fetch({
					query : {
						isattivo: true,
						corsolaurea: {"__type":"Pointer","className":"corsolaurea","objectId":this.model.get('objectId')}
	               	},
	               	success: function(data) {
	                	this.courses = new CoursesView({
							el : $('#corsi ul', this.el),
							model : data
	    				});
	                	this.courses.render();
						var scroll2 = new iScroll('corsiscroller',
	                                  { useTransition:false,
	                                    checkDOMChanges: true,
	                                    hideScrollbar: true,
	                                    fixedScrollbar:true }
	                                );
					}});

				// Docenti del corso di laurea
				this.utenteCollection = new UtenteCollection();
				this.utenteCollection.fetch({
					query : {
						isdocente: true,
						corsolaurea: {"__type":"Pointer","className":"corsolaurea","objectId":this.model.get('objectId')}
	               	},
	               	success: function(data) {
	                	this.courses = new UsersView({
							el : $('#docenti ul', this.el),
							model : data
	    				});
	                	this.courses.render();
						var scroll2 = new iScroll('docentiscroller',
	                                  { useTransition:false,
	                                    checkDOMChanges: true,
	                                    hideScrollbar: true,
	                                    fixedScrollbar:true }
	                                );
					}});

				// Studenti del corso di laurea
				this.utenteCollection = new UtenteCollection();
				this.utenteCollection.fetch({
					query : {
						isdocente: false,
						corsolaurea: {"__type":"Pointer","className":"corsolaurea","objectId":this.model.get('objectId')}
	               	},
	               	success: function(data) {
	                	this.courses = new UsersView({
							el : $('#studenti ul', this.el),
							model : data
	    				});
	                	this.courses.render();
						var scroll2 = new iScroll('studentiscroller',
	                                  { useTransition:false,
	                                    checkDOMChanges: true,
	                                    hideScrollbar: true,
	                                    fixedScrollbar:true }
	                                );
					}});
				
	            return this;
	        },

	        events: {
	            'touchstart .button': 'over',
	            'touchend .button': 'left',
	            'touchcancel .button': 'left',
	            'touchstart button': 'over',
	            'touchend button': 'left',
	            'touchcancel button': 'left',
	            'touchstart .ac-container article.selectable ul li': 'over',
	            'touchend .ac-container article.selectable ul li': 'left',
	            'touchcancel .ac-container article.selectable ul li': 'left'
	        },

	      

	        over: function(event) {
	            $(event.currentTarget).addClass('fake-active');
	        },

	        left: function(event) {
	            $(event.currentTarget).removeClass('fake-active');
	        }
	});

    return CorsolaureaPage;

});

